/*
 * @Description: 创建dom节点
 * @Author: wangqi
 * @Date: 2021-05-10 07:04:51
 * @LastEditTime: 2021-05-10 23:00:51
 */

export default function createElement(vnode) {
    let domNode = document.createElement(vnode.sel);
    // {sel:"div", data:{}, text:"xx", children:[{},...{}], elm: "ele", key:"key"}
    // 按照没有 children元素，只处理text
    if (vnode.text && (!vnode.children || vnode.children.length == 0)) {
        domNode.innerText = vnode.text;
        // 如果存在 children且长度大于0,则需要递归处理
    } else if (Array.isArray(vnode.children) && vnode.children.length > 0) {
        for (let i = 0; i < vnode.children.length; i++) {
            let ch = vnode.children[i];
            let chDom = createElement(ch);
            domNode.appendChild(chDom);
        }
    }

    vnode.elm = domNode;
    return vnode.elm;
}
